import 'package:flutter/material.dart';
import 'package:get/get.dart';

typedef BottomSheetCallback = void Function(int index);

/// 图片数据
final List<String> _picData = [
  "相册",
  "相机",
];

/// 图标数据
final List<IconData> _iconData = [
  Icons.photo,
  Icons.camera_alt,
];

///
enum BottomItemKind {
  image, // 图像
}

/// 打开图片sheet
openImageBottomSheet(BottomSheetCallback? onTap) => _openBottomSheet(BottomItemKind.image, onTap);

_openBottomSheet(BottomItemKind kind, BottomSheetCallback? onTap) {
  switch (kind) {
    case BottomItemKind.image:
      _BottomSheet.showBottomSheet(data: _picData, icons: _iconData, onTap: onTap);
  }
}

class _BottomSheet {
  static void showBottomSheet({
    BuildContext? context,
    required List<String> data,
    required List<IconData> icons,
    BottomSheetCallback? onTap,
  }) {
    if (Get.isBottomSheetOpen == true) {
      Get.back();
    }
    if (context == null) {
      Get.bottomSheet(
        Container(
          height: 120,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: List<Widget>.from(data.map(
              (e) => ListTile(
                leading: Icon(icons[data.indexOf(e)]),
                title: Text(e),
                // titleAlignment: ListTileTitleAlignment.center,
                onTap: () {
                  if (onTap != null) {
                    onTap(data.indexOf(e));
                  }
                  Get.back();
                },
              ),
            )),
          ),
        ),
        backgroundColor: Colors.white,
        elevation: 0,
        isScrollControlled: true,
        shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(16),
            topRight: Radius.circular(16),
          ),
        ),
      );
    } else {
      showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return SizedBox(
            height: 200,
            child: Column(
              children: List<Widget>.from(data.map(
                (e) => ListTile(
                  leading: Icon(icons[data.indexOf(e)]),
                  title: Text(e),
                  onTap: () {
                    if (onTap != null) {
                      onTap(data.indexOf(e));
                    }
                    Navigator.of(context).pop();
                  },
                ),
              )),
            ),
          );
        },
      );
    }
  }
}
